﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Windows;


namespace AI.EditMode
{
    public class AddPath : iEditMode
    {
        Path _CurPath = null;
        Pen _LinePen = new Pen(Color.Red,2);
        Pen _PointPen = new Pen(Color.Blue, 2);

        Point _CurPoint = null;

        public AddPath(AIInfo Form) :
            base(Form)
        {


            foreach (Path path in _Form._MIM._BaseNode._PathList)
            {
                _Form.listPath.Items.Add(path);
            }


            _Form.btnAddPath.Click += new EventHandler(btnAddPath_Click);
            _Form.btnDelPath.Click += new EventHandler(btnDelPath_Click);
            _Form.listPath.SelectedIndexChanged += new EventHandler(listPath_SelectedIndexChanged);


            _Form.btnAddPoint.Click += new EventHandler(btnAddPoint_Click);
            _Form.btnDelPo.Click += new EventHandler(btnDelPo_Click);
            _Form.listPathPoint.SelectedIndexChanged += new EventHandler(listPathPoint_SelectedIndexChanged);

        }

        void listPathPoint_SelectedIndexChanged(object sender, EventArgs e)
        {

            _CurPoint = _Form.listPathPoint.SelectedItem as Point;
        }

        void btnDelPo_Click(object sender, EventArgs e)
        {
            _CurPath.Remove(_CurPoint._index);
            _Form.listPathPoint.Items.Remove(_CurPoint);
        }

        void btnAddPoint_Click(object sender, EventArgs e)
        {
            
        }

        void btnDelPath_Click(object sender, EventArgs e)
        {
            if( _CurPath != null)
            {
                _Form._MIM._BaseNode._PathList.Remove(_CurPath);
                _Form.listPath.Items.Remove(_CurPath);
            }
        }

        void listPath_SelectedIndexChanged(object sender, EventArgs e)
        {
            _CurPath = _Form.listPath.SelectedItem as Path;

            if (_CurPath == null)
                return;
            _Form.listPathPoint.Items.Clear();
            _CurPoint = null;

            foreach (int index in _CurPath)
            {
                Point p = _Form._MIM._BaseNode._PointList[index];
                _Form.listPathPoint.Items.Add(p);
            }
        }

        void btnAddPath_Click(object sender, EventArgs e)
        {
            _CurPath = new Path();

            _Form._MIM._BaseNode._PathList.Add(_CurPath);
            _Form.listPath.Items.Add(_CurPath);
        }

        public override void MouseClick(MouseButtons button, double x, double y)
        {
            if (button == MouseButtons.Right)
            {

            }
            else if (button == MouseButtons.Left)
            {

                Point po = _Form._MIM._BaseNode.GetPoint(x, y);
                _CurPath.Add(po.Index);
                _Form.listPathPoint.Items.Add(po);
            }

        }



        public override void Draw(Graphics g)
        {


            if (_CurPath != null && 1 < _CurPath.Count )
            {
                //g.DrawEllipse(_Cur_Pen, (float)_CurPoint._P.X-3, (float)_CurPoint._P.Y-3, 6, 6);
                
                Point sp = _Form._MIM._BaseNode._PointList[_CurPath[0]];

                for (int index = 1; index < _CurPath.Count; index++)
                {
                    Point ep = _Form._MIM._BaseNode._PointList[_CurPath[index]];
                    _Form.DrawLine(_LinePen, sp.GPSPos, ep.GPSPos);
                    sp = ep;
                }


            }

            if( _CurPoint != null)
            {
                _Form.DrawEllipse(_PointPen, _CurPoint.GPSPos.X, _CurPoint.GPSPos.Y, 8);
            }
            
        }


    }
}
